// $codepro.audit.disable numericLiterals, spaceAroundPeriods, spellCheckComments, spellCheckIdentifiers, temporaryObjectCreation
/**
 * Package gui
 */
package teamrocket.gui; // $codepro.audit.disable packagePrefixNamingConvention


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import teamrocket.domain.MedicalInformation;
import teamrocket.domain.Patient;
import teamrocket.domain.PatientInformation;
import teamrocket.domain.PersonalInformation;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

/**
 * @author James Zhang, Juan Llanes, Stephen Tapper, Will Barr
 * @version 1.0
 */
public class EditPatientFrame extends JFrame {

	/**
	 * Field contentPane.
	 */
	private JPanel contentPane;
	
	/**
	 * Field tbFirstName.
	 */
	private JTextField tbFirstName;
	
	/**
	 * Field tbLastName.
	 */
	private JTextField tbLastName;
	
	/**
	 * Field tbPhone.
	 */
	private JTextField tbPhone;
	
	/**
	 * Field tbEmail.
	 */
	private JTextField tbEmail;
	
	/**
	 * Field tbAge.
	 */
	private JTextField tbAge;
	
	/**
	 * Field tbPharmacy.
	 */
	private JTextField tbPharmacy;
	
	/**
	 * Field tbInsurance.
	 */
	private JTextField tbInsurance;
	
	/**
	 * Field tbAllergies.
	 */
	private JTextField tbAllergies;
	
	/**
	 * Field cbGender.
	 */
	private JComboBox cbGender;

	/**
	 * Field mainWindow.
	 */
	private MainFrame mainWindow;
	
	/**
	 * Field patient.
	 */
	private Patient patient;

	/**
	 * Field update.
	 */
	boolean update; // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.avoidPackageScopeAuditRule
	
	/**
	 * Create the frame.
	
	 * @param mainWindow MainFrame
	 * @wbp.parser.constructor
	 */
	public EditPatientFrame(MainFrame mainWindow) {
		this(mainWindow, null);
		update = false;
	}
	
	/**
	 * Constructor for EditPatientFrame.
	 * @param mainWindow MainFrame
	 * @param patient Patient
	 */
	public EditPatientFrame(MainFrame mainWindow, Patient patient) { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods

		this.mainWindow = mainWindow;
		this.patient = patient;
		
		setTitle("Edit Patient");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 660, 475);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JLabel lblFirstName = new JLabel("First Name:");
		lblFirstName.setBounds(10, 11, 87, 14);
		contentPane.add(lblFirstName);
		
		tbFirstName = new JTextField();
		tbFirstName.setBounds(107, 8, 86, 20);
		contentPane.add(tbFirstName);
		tbFirstName.setColumns(10);
		
		final JLabel lblLastName = new JLabel("Last Name:");
		lblLastName.setBounds(10, 39, 87, 14);
		contentPane.add(lblLastName);
		
		tbLastName = new JTextField();
		tbLastName.setColumns(10);
		tbLastName.setBounds(107, 36, 86, 20);
		contentPane.add(tbLastName);
		
		final JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 65, 87, 14);
		contentPane.add(lblPhone);
		
		tbPhone = new JTextField();
		tbPhone.setColumns(10);
		tbPhone.setBounds(107, 62, 86, 20);
		contentPane.add(tbPhone);
		
		final JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(10, 93, 87, 14);
		contentPane.add(lblEmail);
		
		tbEmail = new JTextField();
		tbEmail.setColumns(10);
		tbEmail.setBounds(107, 90, 86, 20);
		contentPane.add(tbEmail);
		
		final JLabel lblGender = new JLabel("Gender:");
		lblGender.setBounds(10, 121, 87, 14);
		contentPane.add(lblGender);
		
		final JLabel lblAge = new JLabel("Age:");
		lblAge.setBounds(10, 149, 87, 14);
		contentPane.add(lblAge);
		
		tbAge = new JTextField();
		tbAge.setColumns(10);
		tbAge.setBounds(107, 146, 86, 20);
		contentPane.add(tbAge);
		
		cbGender = new JComboBox();
		cbGender.setModel(new DefaultComboBoxModel(new String[] {"Male", "Female"}));
		cbGender.setBounds(107, 121, 86, 20);
		contentPane.add(cbGender);
		
		final JLabel lblPharmacy = new JLabel("Pharmacy:");
		lblPharmacy.setBounds(10, 201, 87, 14);
		contentPane.add(lblPharmacy);
		
		tbPharmacy = new JTextField();
		tbPharmacy.setColumns(10);
		tbPharmacy.setBounds(107, 198, 86, 20);
		contentPane.add(tbPharmacy);
		
		final JLabel lblInsuranceCarrier = new JLabel("Insurance:");
		lblInsuranceCarrier.setBounds(10, 229, 87, 14);
		contentPane.add(lblInsuranceCarrier);
		
		tbInsurance = new JTextField();
		tbInsurance.setColumns(10);
		tbInsurance.setBounds(107, 226, 86, 20);
		contentPane.add(tbInsurance);
		
		final JLabel lblAllergies = new JLabel("Allergies:");
		lblAllergies.setBounds(10, 257, 87, 14);
		contentPane.add(lblAllergies);
		
		tbAllergies = new JTextField();
		tbAllergies.setColumns(10);
		tbAllergies.setBounds(107, 254, 86, 20);
		contentPane.add(tbAllergies);
		
		final JButton btnDeletePatient = new JButton("Delete Patient");
		btnDeletePatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//delete patient
				delete();
			}
		});
		btnDeletePatient.setBounds(10, 398, 122, 23);
		contentPane.add(btnDeletePatient);
		
		final JButton btnSavePatient = new JButton("Save Patient");
		btnSavePatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//save patient
				save();
			}
		});
		btnSavePatient.setBounds(286, 398, 134, 23);
		contentPane.add(btnSavePatient);
		
		final JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cancel();
			}
		});
		btnCancel.setBounds(142, 398, 134, 23);
		contentPane.add(btnCancel);
		if(patient != null) {
			initFields(patient);
			update = true;
		} else {
			btnDeletePatient.setVisible(false);
		}
	}
	
	/**
	 * Method initFields.
	 * @param patient Patient
	 */
	private void initFields(Patient patient) {
		tbFirstName.setText(patient.getFirstName());
		tbLastName.setText(patient.getLastName());
		tbPhone.setText(patient.getPatientInformation().
				getPersonalInformation().getPhoneNumber());
		tbEmail.setText(patient.getPatientInformation().
				getPersonalInformation().getEmailAddress());
		final char gender = patient.getPatientInformation().
				getPersonalInformation().getGender();
		if(gender == 'm' || gender == 'M') {
			cbGender.setSelectedIndex(0);
		} else {
			cbGender.setSelectedIndex(1);
		}
		tbAge.setText(new Integer(patient.getPatientInformation().
				getPersonalInformation().getAge()).toString());
		
		tbPharmacy.setText(patient.getPatientInformation().
				getMedicalInformation().getPharmacy());
		tbInsurance.setText(patient.getPatientInformation().
				getMedicalInformation().getInsuranceCarrier());
		String allergies = "";
		for(String allergy : patient.getPatientInformation().
				getMedicalInformation().getAllergies()) {
			allergies+=allergy + " "; // $codepro.audit.disable stringConcatenationInLoop
		}
		tbAllergies.setText(allergies);
	}
	
	/**
	 * Method delete.
	 */
	private void delete() {
		if(patient != null) {
			try {
				patient.deletePatient();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		mainWindow.rebind();
		setVisible(false);
		mainWindow.setVisible(true);
	}
	
	/**
	 * Method save.
	 */
	private void save() {

		final String first = tbFirstName.getText();
		final String last = tbLastName.getText();
		final String phone = tbPhone.getText();
		final String email = tbEmail.getText();
		String age = tbAge.getText();
		if(age.equalsIgnoreCase("")) {
			age = "0";
		}
		
		final String pharmacy = tbPharmacy.getText();
		final String insurance = tbInsurance.getText();
		final String allergies = tbAllergies.getText();
		
		final char gender = cbGender.getSelectedItem().toString().
				equalsIgnoreCase("male") ? 'm' : 'f';
		
		if(!update) {
			final PersonalInformation personalInformation =
				new PersonalInformation(first, "", phone,
						email, gender, new Integer(age));
			final MedicalInformation medicalInformation =
				new MedicalInformation(pharmacy, insurance,
						new String[]{allergies});
			final PatientInformation patientInformation =
				new PatientInformation(personalInformation, medicalInformation);
			final Patient newPatient = new Patient(Patient.PATIENTDEFPERMS,
					patientInformation, first + last, email, first, last);
			
			newPatient.createPatient();
		}
		else {
			patient.setFirstName(first);
			patient.setLastName(last);
			patient.getPatientInformation().getPersonalInformation().
					setPhoneNumber(phone);
			patient.getPatientInformation().getPersonalInformation().
					setEmailAddress(email);
			patient.getPatientInformation().getPersonalInformation().
					setAge(new Integer(age));
			patient.getPatientInformation().getPersonalInformation().setGender(gender);
			
			patient.getPatientInformation().getMedicalInformation().setPharmacy(pharmacy);
			patient.getPatientInformation().getMedicalInformation().
					setInsuranceCarrier(insurance);
			try {
				patient.updatePatient();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		mainWindow.rebind();
		setVisible(false);
		mainWindow.setVisible(true);
	}

	/**
	 * Method cancel.
	 */
	private void cancel() {
		setVisible(false);
		mainWindow.setVisible(true);
		
	}

}
